package com.yoomiito.app.ui.fragment.my;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yoomiito.app.Constant;
import com.yoomiito.app.adapter.my.MyHistoryMonthAdapter;
import com.yoomiito.app.model.my.MyHistoryMonthBean;
import com.yoomiito.app.model.my.MyHistoryMonthInfo;
import com.yoomiito.app.present.my.fragment.MyHistoryMonthFmPresenter;
import com.yoomiito.app.ui.my.MyHistoryMonthActivity;
import com.yoomiito.app.widget.MyItemDecoration;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;

/**
 * 历史收入---月度详情fm
 */
public class MyHistoryMonthFragment extends MyOrderBaseFragment<MyHistoryMonthFmPresenter> {
//    @BindView(R.id.rv_order)
//    RecyclerView mRecyclerView;
    private MyHistoryMonthAdapter mAdapter;
    private List<MyHistoryMonthBean> mList;
    private int month;
    private int monthData;

    @Override
    public MyHistoryMonthFmPresenter newP() {
        return new MyHistoryMonthFmPresenter();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        if (getArguments() != null) {
            month = getArguments().getInt(Constant.KEY_MONTH, 0);
            monthData = getArguments().getInt(Constant.KEY_MONTH_DATA, 0);
        }
        mList = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.addItemDecoration(new MyItemDecoration(context, RecyclerView.VERTICAL));
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MyHistoryMonthAdapter(mList);
        mRecyclerView.setAdapter(mAdapter);
        getNewData();
    }

    @Override
    public void handleError(ApiException e) {

    }

    public void setHistoryMonthData(MyHistoryMonthInfo data) {
        ((MyHistoryMonthActivity) getActivity()).setData(data);
//        if (currentPage == 1) {
            if (data != null && data.getData() != null) {
                mList.clear();
                MyHistoryMonthBean myHistoryMonthBean = new MyHistoryMonthBean();
                myHistoryMonthBean.setTitle(true);
                myHistoryMonthBean.setMonth(monthData);
                mList.add(myHistoryMonthBean);
                mList.addAll(data.getData());
                mAdapter.setNewData(mList);
                mSmartRefreshLayout.finishRefresh(true);
                mStatusManager.showContent();
            }else {
                mStatusManager.showEmpty();
            }
//        }else {
//            mSmartRefreshLayout.finishLoadMore(true);
//            if (data.getData() == null || data.getData().size() == 0) {
//                mSmartRefreshLayout.finishLoadMoreWithNoMoreData();
//            } else {
//                mAdapter.addData(data.getData());
//            }
//        }
    }

    @Override
    protected boolean useMyItemDecoration() {
        return true;
    }

    @Override
    protected void getNewData() {
        getP().getHistoryMonthData(currentPage, month);
    }

    @Override
    public String getEmptyContent() {
        return "未购买商品";
    }

    @Override
    public boolean isRefresh() {
        return false;
    }
}
